<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import useUserStore from '@/store/modules/user.ts'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/times'
import { validateUsername, validatePassword } from '@/utils/validators'

const userStore = useUserStore()
const $router = useRouter()
const $route = useRoute()

const loginForm = ref({
    username: 'admin',
    password: '111111'
})
// 控制登录按钮是否在加载状态
let loading = ref(false)
// 获取当前的时间区间
const time = getTime()

const ruleFormRef = ref()

// 登录
function loginHandle() {
    ruleFormRef.value.validate((isValid: boolean) => {
        // 检查表单是否校验通过，当校验通过再发送请求
        if (isValid) {
            // 通知仓库发送登录请求
            // 分别处理请求成功和失败的结果
            loading.value = true
            userStore.userLogin(loginForm.value).then(() => {
                loading.value = false
                // 登录成功后，跳转到退出页面的路由
                $router.push($route.query.redirect as string || '/')
                ElNotification({
                    type: 'success',
                    message: '欢迎来到硅谷甄选',
                    title: `Hi! ${time}好`
                })
            }).catch(error => {
                loading.value = false
                ElNotification({
                    type: 'error',
                    message: error
                })
            })
        }
    })


}

// 定义用户名和密码校验规则
const rules = ref({
    username: [
        { validator: validateUsername, trigger: 'change' }

    ],
    password: [
        { validator: validatePassword, trigger: 'change' }
    ]
})
</script>

<template>
    <div class="login-container">
        <el-row>
            <el-col :span="12" :xs="0"></el-col>
            <el-col :span="12" :xs="24">
                <div class="login-form">
                    <h1 class="title">Hello</h1>
                    <h2 class="title2">欢迎来到硅谷甄选</h2>
                    <el-form :model="loginForm" :rules="rules" ref="ruleFormRef">
                        <el-form-item prop="username">
                            <el-input v-model="loginForm.username" placeholder="请输入用户名" :prefix-icon="User"
                                class="login-input login-input-username" />
                        </el-form-item>
                        <el-form-item prop="password">
                            <el-input v-model="loginForm.password" placeholder="请输入密码" :prefix-icon="Lock"
                                type="password" show-password class="login-input" />
                        </el-form-item>
                        <el-form-item>
                            <el-button :loading="loading" @click="loginHandle" type="primary"
                                class="login-btn">登录</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </el-col>
        </el-row>
    </div>
</template>

<style lang="scss" scoped>
.login-container {
    width: 100%;
    height: 100vh;
    background: url('@/assets/images/background.jpg') no-repeat center center;
    background-size: cover;

    .login-form {
        padding: 40px;
        position: relative;
        top: 25vh;
        width: 80%;
        background: url('@/assets/images/login_form.png') no-repeat center center;
        background-size: cover;

        .title {
            color: white;
            font-size: 50px;
        }

        .title2 {
            color: white;
            font-size: 24px;
        }

        .login-btn {
            width: 100%;
            margin: auto;
        }

        :deep(.el-form-item) {
            margin-bottom: 26px;
        }

        .login-input-username {
            margin-top: 30px;
        }


    }
}
</style>